home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pcb / copydat1.zip / COPYDATE.C next >
C/C++ Source or Header  |  1992-11-11  |  6KB  |  169 lines

  1. /*  COPYDATE version 1.01        Copyright (C) 1992 by Jim Robeson
  2.  *                                                     11/11/92
  3.  *  Function:
  4.  *    Read a PCBoard DIR LIST file (arg-1),
  5.  *      and copy it to a file (arg-2),
  6.  *      changing the date field to what is found in (arg-3).
  7.  *      NOTE: the date MUST be formatted MM-DD-YY
  8.  *
  9.  *  Other uses:
  10.  *    None.
  11.  *
  12.  *  Execution:
  13.  *    Run from command line or .BAT:
  14.  *      COPYDATE drv:\path\in-file-name drv:\path\out-file-name MM-DD-YY
  15.  *    If run without arguments, a bit of help appears.
  16.  *
  17.  *  Compiled with:
  18.  *    Borland's Turbo C 2.0.
  19.  *    tcc copydate
  20.  *
  21.  *  Disclaimer:
  22.  *    This program is contributed to the Public Domain.  It can be
  23.  *    freely used, modified and/or distributed by anyone. The only
  24.  *    thing I ask is that you remember that I am not responsible
  25.  *    for anything that might go wrong through the use of this
  26.  *    program.  I have tested the program enough for my own use.
  27.  *    I also realize that bugs can appear in most every program.
  28.  *    Therefore, YOU USE THIS PROGRAM AT YOUR OWN RISK.
  29.  *
  30.  *  To-do:
  31.  *    do a "logical" test on the date
  32.  *
  33.  *  Enjoy,
  34.  *  Jim Robeson
  35.  *  The Cricket BBS, Pacific Grove CA,  408-373-3773
  36.  */
  37.  
  38. #include "stdio.h"                        /* Standard I/O definitions */
  39. #include "string.h"
  40.  
  41. #define TRUE 1
  42. #define FALSE 0
  43.  
  44. /* test to see if 'ch' is a valid DOS character */
  45. #define vld_dos(ch)  ( (( (ch) >= 'A' && (ch) <= 'Z' ) || \
  46.                         ( (ch) >= '0' && (ch) <= '9' ) || \
  47.                         (ch) == '!' || \
  48.                         (ch) == '$' || \
  49.                         (ch) == '@' || \
  50.                         (ch) == '#')   \
  51.                        ? 1             \
  52.                        : 0 )
  53. /* test to see if 'ch' is numeric..... <compare   to isdigit(c)> */
  54. #define is_digit(ch)   ( ( (ch) < '0' || (ch) > '9' ) \
  55.                          ? 0                          \
  56.                          : ch )
  57.  
  58. void showhelp ( );         /* prototype */
  59.  
  60.   FILE *infile;            /* the IN file */
  61.   FILE *outfile;           /* the OUT file */
  62.   char inbuf[BUFSIZ];      /* line buffer for reading from file */
  63.                            /* BUFSIZ = 512 in stdio.h */
  64.   int ipt,opt,i;           /* index pointers */
  65.  
  66. /* ========================================= */
  67. /* ===  MAIN                             === */
  68. /* ========================================= */
  69. main(int argc, char *argv[])       /* main reads command args  */
  70. {
  71.   char *progname;
  72.   char *filein;
  73.   char *fileout;
  74.   char *date_buf;
  75.  
  76. /*  Display a little how-to "help" if arguments are null or improper  */
  77.  
  78.   if (argc != 4)           /*  should be 3 args + prognam */
  79.      { if (argc != 1)
  80.          printf("\nERROR: needs 3 arguments\n");
  81.        showhelp();
  82.        exit (1); }               /* exit with errorlevel=1 */
  83.  
  84.   progname = argv[0];
  85.   filein   = argv[1];
  86.   fileout  = argv[2];
  87.   date_buf = argv[3];
  88.  
  89.     /* test for valid date in arg-3  */
  90.   if (!(is_digit(date_buf[0]) &&      /* pos 1 is numeric       */
  91.         is_digit(date_buf[1]) &&      /* pos 2 is numeric       */
  92.         date_buf[2] == '-' &&         /* pos 3 = '-'            */
  93.         is_digit(date_buf[3]) &&      /* pos 4 is numeric       */
  94.         is_digit(date_buf[4]) &&      /* pos 5 is numeric       */
  95.         date_buf[5] == '-' &&         /* pos 6 = '-'            */
  96.         is_digit(date_buf[6]) &&      /* pos 7 is numeric       */
  97.         is_digit(date_buf[7]) ))      /* pos 8 is numeric       */
  98.      { printf("\nERROR: the date field (arg-3) must be formatted MM-DD-YY\n");
  99.        showhelp();
  100.        exit (1); }               /* exit with errorlevel=1 */
  101.  
  102.   printf("\nCOPYDATE IS RUNNING.  Input = %s, Output = %s,\n",filein,fileout);
  103.   printf("                      date = %s.\n",date_buf);
  104.  
  105.   infile = fopen(filein,"r");          /* open the input file */
  106.   if (infile == NULL)
  107.     {
  108.     fprintf(stderr,"\n\007%s can't open the INPUT file: %s.\n",progname,filein);
  109.     exit (1);
  110.     }
  111.  
  112.   outfile = fopen(fileout,"w");          /* open the output file */
  113.   if (outfile == NULL)
  114.     {
  115.     fprintf(stderr,"\n\007%s can't open the OUTPUT file: %s.\n",progname,fileout);
  116.     exit (1);
  117.     }
  118.  
  119.  /* ========================  copy the file ========================= */
  120.   while ( fgets(inbuf,BUFSIZ,infile) != NULL )  /* TOP-OF-LOOP */
  121.     {
  122.     /* test for line 1 of a file description:     */
  123.     if (vld_dos(inbuf[0]) &&        /* pos 1 valid DOS chars & */
  124.         is_digit(inbuf[20]) &&      /* pos 21 is numeric       */
  125.         inbuf[25] == '-' &&         /* pos 26 = '-'            */
  126.         inbuf[28] == '-')           /* pos 29 = '-'            */
  127.  
  128.       { /* ==== FILE DESCRIPTION LINE # 1 FOUND ======== */
  129.  
  130.         ipt = 0;           /* move the new date in */
  131.         opt = 23;
  132.         for (i=0; i < 8; i++)
  133.           { inbuf[opt] = date_buf[ipt];
  134.             ipt++;
  135.             opt++;
  136.           }
  137.  
  138.       }   /* ====== end  FILE #1 LINE REFORMAT =========== */
  139.  
  140.     fprintf(outfile,"%s",inbuf);
  141.  
  142.     } /* end of while at TOP-OF-LOOP */
  143.  
  144.   /* === Since we dropped through,  ============== */
  145.   /* === implies end of file found. ============== */
  146.  
  147.   fclose(infile);
  148.   fclose(outfile);
  149.  
  150.   printf("COPYDATE IS FINISHED.\n");
  151.   exit (0);      /* job done, get out, errorvalue = 0 */
  152.  
  153. }  /* ------------- end of main ------------------------------- */
  154.  
  155. /* ========================================= */
  156. /* ===  SHOWHELP                         === */
  157. /* ========================================= */
  158. void showhelp ()
  159. {
  160.   printf("\nCOPYDATE:\n");
  161.   printf("    Copy PCBoard DIR files (descriptions) replacing the date field.\n");
  162.   printf("    by Jim Robeson, 11-11-92\n\n");
  163.   printf("USAGE:\n");
  164.   printf("    COPYDATE drv:\path\in-file-name drv:\path\out-file-name MM-DD-YY \n");
  165.   return;
  166. }  /* ------------- end of showhelp --------------------------- */
  167.  
  168. /*------------- End of COPYDATE.C ------------------------------------*/
  169.